package com.hd.controller.code;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hd.controller.base.BaseController;
import com.hd.entity.code.CodeCategory;
import com.hd.entity.code.CodeData;
import com.hd.service.code.CodeCategoryService;
import com.hd.service.code.CodeDataService;
import com.hd.util.StringUtils;
import com.hd.util.result.PageInfo;
/**
 * 数据字典
 * @author hzhh123
 *
 */
@RequestMapping("/code")
@Controller
public class CodeController extends BaseController{
	@Autowired
	private CodeDataService codeDataService;
	@Autowired
	private CodeCategoryService codeCategoryService;
	/**
	 * 跳转到数据字典类别页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/codeCategory/toList")
	public String toCodeCategoryList(Model model) {
		return "jsp/code/codeCategory/codeCategoryList";
	}
/*	*//**
	 * 返回所有数据字典类别
	 * @return
	 *//*
	@RequestMapping("/findAllCodeCategory")
	@ResponseBody
	public Object findAllCodeCategory() {
		List<CodeCategory> codeCategoryList = codeCategoryService.findAll();
		return renderSuccess(codeCategoryList);
	}*/
	
	/**
	 * 跳转到数据字典类别添加页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/codeCategory/toAdd")
	public String toAddCodeCategory(Model model) {
		return "jsp/code/codeCategory/codeCategoryAdd";
	}
	
	@RequestMapping("/codeCategory/add")
	@ResponseBody
	public Object addCodeCategory(CodeCategory codeCategory) {
		boolean b=codeCategoryService.validCategory(codeCategory.getCategory());
		if(b) {
			codeCategoryService.save(codeCategory);
			return renderSuccess("添加成功！");
		}
		return renderError("添加失败！");
	}
	  /**
     * 数据字典类别列表
     * @param page
     * @param rows
     * @param sort
     * @param order
     * @return
     */
    @RequestMapping(value="/codeCategory/dataGrid")
    @ResponseBody
    public Object dataGrid(CodeCategory codeCategory, Integer page, Integer rows, String sort, String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        Map<String,Object>condition=new HashMap<String,Object>();
        pageInfo.setCondition(condition);
        codeCategoryService.selectDataGrid(pageInfo);
        return pageInfo;
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/codeCategory/delete/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@PathVariable("id") String id) {
        codeCategoryService.delete(id);
        return renderSuccess("删除成功！");
    }
    
    /**
     * 编辑页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/codeCategory/toEdit/{id}")
	public String editPage(Model model, @PathVariable("id")String id) {
	 CodeCategory codeCategory = codeCategoryService.get(id);
        model.addAttribute("codeCategory", codeCategory);
        return "jsp/code/codeCategory/codeCategoryEdit";
    }
    /**
     * 编辑
     * @param codeCategory
     * @return
     */
    @RequestMapping(value = "/codeCategory/edit",method = RequestMethod.POST)
    @ResponseBody
    public Object edit(CodeCategory codeCategory) {
        codeCategoryService.update(codeCategory);
        return renderSuccess("编辑成功！");
    }
    /**
     * 查看页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/codeCategory/view/{id}")
    public String viewPage(Model model, @PathVariable("id") String id) {
        CodeCategory codeCategory = codeCategoryService.get(id);
        model.addAttribute("codeCategory", codeCategory);
        return "jsp/code/codeCategory/codeCategoryView";
    }
    
	/**
	 * 跳转到数据字典页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/codeData/toList")
	public String toCodeDataList(Model model) {
		List<CodeCategory> codeCategoryList = codeCategoryService.findAll();
		model.addAttribute("codeCategoryList", codeCategoryList);
		return "jsp/code/codeData/codeDataList";
	}
	/**
     * 数据字典列表
     * @param page
     * @param rows
     * @param sort
     * @param order
     * @return
     */
    @RequestMapping(value="/codeData/dataGrid")
    @ResponseBody
    public Object dataGrid(CodeData codeData, Integer page, Integer rows, String sort, String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        Map<String,Object>condition=new HashMap<String,Object>();
        System.out.println(codeData.getCategory()==null);
        if(StringUtils.isNotNull(codeData.getCategory())) {
        	condition.put("category", codeData.getCategory());
        }
        pageInfo.setCondition(condition);
        codeDataService.selectDataGrid(pageInfo);
        return pageInfo;
    }
    
    /**
	 * 跳转到数据字典添加页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/codeData/toAdd")
	public String toAddCodeData(Model model) {
		List<CodeCategory> codeCategoryList = codeCategoryService.findAll();
		model.addAttribute("codeCategoryList", codeCategoryList);
		return "jsp/code/codeData/codeDataAdd";
	}
	
	/**
	 * 添加数据字典
	 * @param codeData
	 * @return
	 */
    @RequestMapping("/codeData/add")
	@ResponseBody
	public Object addCodeData(CodeData codeData) {
		boolean b=codeDataService.validCode(codeData.getCode(),codeData.getCategory());
		if(b) {
			codeDataService.save(codeData);
			return renderSuccess("添加成功！");
		}
		return renderError("添加失败！"+codeData.getCategoryName()+"的编码已存在！");
	}

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/codeData/delete/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Object deleteForCodeData(@PathVariable("id") String id) {
        codeDataService.delete(id);
        return renderSuccess("删除成功！");
    }
    
    /**
     * 编辑页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/codeData/toEdit/{id}")
	public String toEditCodeData(Model model, @PathVariable("id")String id) {
    	CodeData codeData = codeDataService.get(id);
        model.addAttribute("codeData", codeData);
        List<CodeCategory> codeCategoryList = codeCategoryService.findAll();
		model.addAttribute("codeCategoryList", codeCategoryList);
        return "jsp/code/codeData/codeDataEdit";
    }
    
    /**
     * 查看页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/codeData/view/{id}")
    public String codeDataView(Model model, @PathVariable("id") String id) {
        CodeData codeData = codeDataService.get(id);
        model.addAttribute("codeData", codeData);
        return "jsp/code/codeData/codeDataView";
    }
    
    /**
     * 编辑
     * @param codeData
     * @return
     */
    @RequestMapping(value = "/codeData/edit",method = RequestMethod.POST)
    @ResponseBody
    public Object editCodeData(CodeData codeData) {
        CodeData vo=codeDataService.get(codeData.getId());
        boolean b=true;
        if(!vo.getCategory().equals(codeData.getCategory())||!vo.getCode().equals(codeData.getCode())) {
        	 b=codeDataService.validCode(codeData.getCode(),codeData.getCategory());
        }
		if(b) {
			codeDataService.update(codeData);
			return renderSuccess("修改成功！");
		}
		return renderError("修改失败！编码被占用！");
    }
    
    /**
     * 根据代码类别跳转页面
     * @param category
     * @param model
     * @return
     */
    @RequestMapping("/toCodeDataByCategory")
    public String toCodeDataByCategory(String category,Model model) {
    	model.addAttribute("type", category);
    	return "jsp/code/codeDataByCategoryList";
    }
  
    /**
	 * 根据代码类别跳转到添加頁面
	 * @param model
	 * @return
	 */
	@RequestMapping("/toAddCodeDataByCategory")
	public String toAddCodeDataByCategory(String category,Model model) {
		model.addAttribute("category", category);
		model.addAttribute("type", category);
		CodeCategory codeCategory=codeCategoryService.getBycategory(category);
		model.addAttribute("categoryName", codeCategory.getCategoryName());
		return "jsp/code/codeDataByCategoryAdd";
	}
	 /**
		 * 根据代码类别跳转到編輯頁面
		 * @param model
		 * @return
		 */
		@RequestMapping("/toEditCodeDataByCategory")
		public String toEditCodeDataByCategory(String id,String category,Model model) {
			model.addAttribute("category", category);
			model.addAttribute("type", category);
			CodeCategory codeCategory=codeCategoryService.getBycategory(category);
			model.addAttribute("categoryName", codeCategory.getCategoryName());
			CodeData codeData = codeDataService.get(id);
			model.addAttribute("codeData", codeData);
			return "jsp/code/codeDataCategoryEdit";
		}
}
